package com.xusheng.cli.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.taskRecordModule.TaskRecord;
import com.xusheng.cli.service.TaskRecordService;
import com.xusheng.cli.tools.CurrentPlayerUserUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 任务中心（任务记录）(TaskRecord)表控制层
 *
 * @author whr
 * @since 2023-11-27 13:30:35
 */
@Validated
@RestController
@RequestMapping("/user/v1/taskRecord")
public class TaskRecordController {
    /**
     * 服务对象
     */
    @Resource
    private TaskRecordService taskRecordService;

    /**
     * 查询所有数据
     *
     * @param taskRecord 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-11-27 13:30:35
     */
    @GetMapping("queryAll")
    public Result<List<TaskRecord>> queryAll(TaskRecord taskRecord) {
        taskRecord.setUserId(CurrentPlayerUserUtil.getUserId());
        List<TaskRecord> list = taskRecordService.list(new QueryWrapper<>(taskRecord));
        for (TaskRecord record : list) {
            record.setGold(1);
            switch (record.getType()) {
                case "record":
                    record.setContent("观看10部影片");
                    break;
                case "ad":
                    record.setContent("点击广告");
                    break;
                case "code":
                    record.setContent("保存推广二维码");
                    break;
                case "favorite":
                    record.setContent("收藏5部影片");
                    break;
                case "comment":
                    record.setContent("评论1部影片");
                    break;
            }
        }
        return Result.success(list);
    }

    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param taskRecord 查询实体
     * @return 所有数据
     * @author whr
     * @since 2023-11-27 13:30:35
     */
    @GetMapping("queryByPage")
    public Result<MyPage<TaskRecord>> queryByPage(MyPage<TaskRecord> page, TaskRecord taskRecord) {
        taskRecord.setUserId(CurrentPlayerUserUtil.getUserId());
        MyPage<TaskRecord> page1 = taskRecordService.page(page, new QueryWrapper<>(taskRecord));
        List<TaskRecord> list = page1.getRecords();
        for (TaskRecord record : list) {
            record.setGold(1);
            switch (record.getType()) {
                case "record":
                    record.setContent("观看10部影片");
                    break;
                case "ad":
                    record.setContent("点击广告");
                    break;
                case "code":
                    record.setContent("保存推广二维码");
                    break;
                case "favorite":
                    record.setContent("收藏5部影片");
                    break;
                case "comment":
                    record.setContent("评论1部影片");
                    break;
            }
        }
        return Result.success(page1);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author whr
     * @since 2023-11-27 13:30:35
     */
    @GetMapping("queryOne/{id}")
    public Result<TaskRecord> selectOne(@PathVariable Serializable id) {
        return Result.success(taskRecordService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param taskRecord 实体对象
     * @return 新增结果
     * @author whr
     * @since 2023-11-27 13:30:35
     */
    @PostMapping("insert")
    public Result<Object> insert(@Validated(Save.class) @RequestBody TaskRecord taskRecord) {
        return Result.isSuccess(taskRecordService.save(taskRecord));
    }

    /**
     * 修改数据
     *
     * @param taskRecord 实体对象
     * @return 修改结果
     * @author whr
     * @since 2023-11-27 13:30:35
     */
    @PutMapping("update")
    public Result<Object> update(@Validated(Update.class) @RequestBody TaskRecord taskRecord) {
        return Result.isSuccess(taskRecordService.updateById(taskRecord));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author whr
     * @since 2023-11-27 13:30:35
     */
    @DeleteMapping("delete/{id}")
    public Result<Object> delete(@PathVariable Long id) {
        TaskRecord taskRecord = new TaskRecord();
        taskRecord.setId(id);
        return Result.isSuccess(taskRecordService.removeById(taskRecord));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author whr
     * @since 2023-11-27 13:30:35
     */
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        return Result.isSuccess(taskRecordService.batchDelete(idList, new TaskRecord()));
    }
}

